package com.mftour.common.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.mftour.common.Constant;
import com.mftour.moudles.entity.User;
import com.mftour.moudles.service.UserService;

public class OnlineListener implements HttpSessionListener {

	@Override
	/* Session创建事件 */
	public void sessionCreated(HttpSessionEvent arg0) {
	}

	@Override
	/* Session失效事件 */
	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		User loginUser = (User) sessionEvent.getSession().getAttribute(Constant.SESSION_LOGIN_USER);
		if (loginUser != null) {
			User u = new User();
			u.setUserId(loginUser.getUserId());
			u.setLiveStatus("0");
			WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sessionEvent.getSession().getServletContext());
			UserService userSer = (UserService) context.getBean("userService");

			userSer.updateByPrimaryKeySelective(u);
		}
	}

}
